<template>
	<view class="content" style="padding-top: 120rpx;">
		<u-navbar title="填写盲板作业票" @leftClick="leftClick" :fixed="true" :safeAreaInsetTop="true" :autoBack="true" height="88rpx">
			<view class="u-nav-slot" slot="right" v-if="isApproval" @click="toProcess">
				流程图
			</view>
		</u-navbar>
<!--		<view class="showTitle">-->
<!--			<view>作业票基本信息：</view>-->
<!--		</view>-->
		<view class="showTitle">
			<!-- 基本信息 -->
			<u-collapse @change="change" @close="close" @open="open" :value="['workInfo']">
				<u-collapse-item title="作业信息" name="workInfo" ref="workInfo">
					<u--form labelPosition="left" :model="formData" :rules="rules" ref="form" labelWidth="100px">
						<u-form-item v-if="id" label="作业证编号" prop="number" borderBottom>
							<u--input v-model="formData.number" border="bottom" disabled></u--input>
						</u-form-item>
						<u-form-item label="申请人" prop="applicant" borderBottom>
							<u--input v-model="formData.applicant" border="bottom" disabled></u--input>
						</u-form-item>
						<u-form-item label="申请单位" prop="applicationUnit" borderBottom>
							<u--input v-model="formData.applicationUnit" border="bottom" disabled></u--input>
						</u-form-item>
						<u-form-item label="设备管道名称" prop="name" borderBottom>
							<u--input v-model="formData.name" border="bottom" placeholder="请输入" :disabled='viewDisabled || appendDisabled || disable'></u--input>
						</u-form-item>
						<u-form-item label="介质" prop="bindPlateMedium" borderBottom>
							<u--input v-model="formData.bindPlateMedium" border="bottom" placeholder="请输入" :disabled='viewDisabled || appendDisabled || disable'></u--input>
						</u-form-item>
						<u-form-item label="温度" prop="bindPlateTemperature" borderBottom>
							<u--input v-model="formData.bindPlateTemperature" border="bottom" placeholder="请输入" :disabled='viewDisabled || appendDisabled || disable'>
							</u--input>
						</u-form-item>
						<u-form-item label="压力" prop="bindPlatePressure" borderBottom>
							<u--input v-model="formData.bindPlatePressure" border="bottom" placeholder="请输入" :disabled='viewDisabled || appendDisabled || disable'></u--input>
						</u-form-item>
						<u-form-item label="盲板材质" prop="bindPlateMaterial" borderBottom>
							<u--input v-model="formData.bindPlateMaterial" border="bottom" placeholder="请输入" :disabled='viewDisabled || appendDisabled || disable'></u--input>
						</u-form-item>
						<u-form-item label="盲板规格" prop="bindPlateSpecification" borderBottom>
							<u--input v-model="formData.bindPlateSpecification" border="bottom" placeholder="请输入" :disabled='viewDisabled || appendDisabled || disable'>
							</u--input>
						</u-form-item>
						<u-form-item label="盲板编号" prop="bindPlateNumber" borderBottom>
							<u--input v-model="formData.bindPlateNumber" border="bottom" placeholder="请输入" :disabled='viewDisabled || appendDisabled || disable'></u--input>
						</u-form-item>
						<u-form-item label="堵实施时间" prop="startTime" borderBottom @click="selectTime(1)">
							<u--input v-model="formData.startTime" disabled disabledColor="#ffffff" placeholder="请选择"
								border="none"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
						<u-form-item v-if="formData.startTime" label="抽实施时间" prop="endTime" borderBottom @click="selectTime(2)">
							<u--input v-model="formData.endTime" disabled disabledColor="#ffffff" placeholder="请选择"
								border="none"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
						<!-- 			<u-form-item label="堵作业人" prop="dznr">
							<u--input v-model="formData.dznr" border="bottom" placeholder="请输入"
								:disabled='viewDisabled || appendDisabled'>
							</u--input>
						</u-form-item> -->
						<view v-for="(item,index) in formData.personList" :key="index">
              <u-form-item label="作业人" prop="item.workerName">
                <u--input v-model="item.workerName" border="bottom" placeholder="请输入"
                          :disabled='viewDisabled || appendDisabled || disable'>
                </u--input>
              </u-form-item>
              <u-form-item label="作业人证件">
                <u-upload :fileList="item.certification"
                          @afterRead="(event) =>afterRead(event,item.certification)" @delete="deletePic"
                          :maxCount="1" :previewFullImage="true" :disabled='viewDisabled || appendDisabled || disable'>
                </u-upload>
                <u-icon class="iconStyle" name="minus-circle" v-if="index != 0" color="#2979ff" size="28" @click="deleteRow(index)"></u-icon>
              </u-form-item>
              <u-button type="primary" icon="plus" text="添加作业人" v-if="index == formData.personList.length - 1" @click="addPerson"
                        :disabled='viewDisabled || appendDisabled || disable'></u-button>
						</view>
						<!-- <u-form-item label="堵监护人" prop="dznr">
							<u--input v-model="formData.dznr" border="bottom" placeholder="请输入"
								:disabled='viewDisabled || appendDisabled'>
							</u--input>
						</u-form-item> -->
<!--						<u-form-item label="监护人" prop="guardian">-->
<!--							<u&#45;&#45;input v-model="formData.guardian" border="bottom" placeholder="请输入"-->
<!--								:disabled='viewDisabled || appendDisabled'>-->
<!--							</u&#45;&#45;input>-->
<!--						</u-form-item>-->
						<!-- <u-form-item label="生产单位作业指挥" prop="dznr">
							<u--input v-model="formData.dznr" border="bottom" placeholder="请输入"
								:disabled='viewDisabled || appendDisabled'>
							</u--input>
						</u-form-item>
						<u-form-item label="作业单位负责人" prop="dznr">
							<u--input v-model="formData.dznr" border="bottom" placeholder="请输入"
								:disabled='viewDisabled || appendDisabled'>
							</u--input>
						</u-form-item> -->
						<u-form-item label="涉及的其他特殊作业" prop="otherLicenceName" borderBottom @click="select(3)">
							<u--input v-model="formData.otherLicenceName" disabled disabledColor="#ffffff"
								placeholder="请选择" border="none"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
						<!-- <u-form-item label="盲板位置图编号" prop="dznr">
							<u--input v-model="formData.dznr" border="bottom" placeholder="请输入"
								:disabled='viewDisabled || appendDisabled'>
							</u--input>
						</u-form-item> -->
						<u-form-item label="盲板位置图" prop="bindPlateImage">
							<u-upload :fileList="formData.bindPlateImage"
								@afterRead="(event) =>afterRead(event,formData.bindPlateImage)" :disabled='viewDisabled || disable'
								@delete="deletePic" :maxCount="1" :previewFullImage="true">
							</u-upload>
						</u-form-item>
						<u-form-item label="风险辨识结果" prop="identificationResult">
							<u--input v-model="formData.identificationResult" border="bottom" placeholder="请输入"
								:disabled='viewDisabled || appendDisabled || disable'>
							</u--input>
						</u-form-item>
					</u--form>
				</u-collapse-item>
			</u-collapse>
		</view>
<!--		<view class="showTitle">-->
<!--			<view>安全措施：</view>-->
<!--		</view>-->
		<view class="showTitle">
			<u-collapse @change="change" @close="close" @open="open" :value="['safe']" ref="safe">
				<u-collapse-item title="安全措施" name="safe">
					<view v-for="(item,index) in safeMeasuresList" :key="index">
						<view>
							<view>{{index + 1}}、{{item.content}}</view>
							<view>
								<u-radio-group v-model="item.radioStatus">
									<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item2, index2) in statusList"
											 :disabled='viewDisabled || appendDisabled || disable' :key="index2" :label="item2.name"
											 :name="item2.name">
									</u-radio>
								</u-radio-group>
							</view>
						</view>
						<takePhoto :purpose="'safeMeasuresPhoto'"
									:index="index" :imgurl='item.photo'
									@snapHandler="onImgUploadHandler">
						</takePhoto>
						
					</view>
				</u-collapse-item>
			</u-collapse>
		</view>
		<view class="showTitle">
			<u-collapse @change="change" @close="close" @open="open" :value="['peopleInfo']" ref='personInfo'>
				<u-collapse-item title="人员信息" name="peopleInfo">
					<u--form labelPosition="left" :model="formData" ref="form3" labelWidth="150px">
						<u-form-item label="安全交底人" prop="applicant" borderBottom>
							<u--input v-model="formData.applicant" border="bottom" placeholder="请输入" disabled>
							</u--input>
						</u-form-item>
					</u--form>
					<u--form labelPosition="left" :model="formData" ref="form5" labelWidth="150px">

            <view v-for="(item,index) in formData.personList" :key='index'>
              <u-form-item :label="'接受交底人（'+item.workerName+'）'" prop="receiver1" borderBottom>
                <signature border="bottom" style="width: 100%;height:100%;" :disabled='viewDisabled || appendDisabled'
                           :purpose="'receiver1'" :index='index' :imgurl='item.signature'
                           @signatureHandler="onImgUploadHandler">
                </signature>
              </u-form-item>
            </view>
            <u-form-item label="监护人" prop="guardian">
              <signature border="bottom" style="width: 100%;height:100%;"  :disabled='viewDisabled || appendDisabled'
                         :purpose="'guardian'" :index='index' :imgurl='formData.guardian'
                         @signatureHandler="onImgUploadHandler"
              >
              </signature>
            </u-form-item>
					</u--form>
				</u-collapse-item>
			</u-collapse>
		</view>
		<view class="showTitle">
			<u-collapse @change="change" @close="close" @open="open" :value="['checkPeopleInfo']" ref='checkPeopleInfo'>
				<u-collapse-item title="审核人员信息" name="checkPeopleInfo">
					<u--form labelPosition="left" :model="formData4" :rules="rules4" ref="form4" labelWidth="100px">
						<u-form-item label="许可人" prop="licensor" borderBottom @click="selectApproval(1)">
							<u--input v-model="formData4.licensor" disabled disabledColor="#ffffff" placeholder="请选择"
								border="none"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
						<u-form-item label="签发人" prop="signer" borderBottom @click="selectApproval(2)">
							<u--input v-model="formData4.signer" disabled disabledColor="#ffffff" placeholder="请选择"
								border="none"></u--input>
							<u-icon slot="right" name="arrow-right"></u-icon>
						</u-form-item>
					</u--form>
				</u-collapse-item>
			</u-collapse>
		</view>

		<!-- 审核签名 -->
		<view class="showTitle" v-if="isApproval">
			<u-collapse @change="change" @close="close" @open="open" :value="['sign']">
				<u-collapse-item title="人员签名" name="sign">
					<u--form labelPosition="left" :model="formData5" ref="form5" labelWidth="100px">
						<view>
							<u-form-item label="申请单位意见" prop="sqdwyj">
								<u--input v-model="formData5.sqdwyj" border="bottom" placeholder="请输入">
								</u--input>
							</u-form-item>
						</view>
					</u--form>
				</u-collapse-item>
			</u-collapse>
		</view>
		<view class="showTitle" v-if="!viewDisabled || isApproval">
			<view class="btn" v-if="disable">
				<u-button type="success" shape="circle" :hairline="true" :plain="true" text="驳回" @click="rejectWork">
				</u-button>
				<u-button type="primary" shape="circle" :plain="true" text="通过" @click="approvalAgree">
				</u-button>
			</view>
			<view class="btn" v-else>
				<u-button type="success" shape="circle" :hairline="true" :plain="true" text="保存" @click="save">
				</u-button>
				<u-button type="primary" shape="circle" :plain="true" text="提交审核" @click="submit"></u-button>
			</view>
		</view>
		<!-- 下拉选项 -->
		<u-action-sheet :show="show" :actions="arr" :title="title" @close="show = false" @select="levelSelect">
		</u-action-sheet>
		<!-- 时间 -->
		<view class="time">
			<!-- <u-calendar :show="showTime" :mode="mode" @confirm="confirm" @close="showTime = false"></u-calendar> -->
			<u-datetime-picker :show="showTime" v-model="timeValue" :minDate="timeType == 2 ? getMinDate(formData.startTime) : ''" mode="datetime" @confirm="confirm"
				@cancel="showTime = false" ref="datetimePicker"></u-datetime-picker>
		</view>
		<!-- 树 -->
		<u-popup :show="showApproval" @close="closeTree" @open="open" :closeable="true">
			<view class="popContainer">
				<view class="treeContainer">
					<xiaolu-tree :checkList="checkList" :props="props" @sendValue="confirmApproval" :isCheck="true"
						:searchIf='false' :treeNone="treeData"></xiaolu-tree>
				</view>
			</view>
		</u-popup>
		<u-toast ref="uToast" type="success" message="提交成功"></u-toast>
	</view>
</template>

<script>
	import {
		addTicket,
		getOtherLicence,
		getSafe,
		getDetail,
		editSave,
		approval
	} from '@/api/workTicket/index.js'
	import { onSnapUploadHandler } from '../common/onSnapUploadHandler'
	import xiaoluTree from '../../components/xiaolu-tree/tree.vue'
	import headerBar from '../../components/header/index.vue'
  import dayjs from 'dayjs'

	export default {
		components: {
			headerBar,
			xiaoluTree
		},
		created() {
			this.isEdit()
			this.getOtherLicence()
		},
		mounted() {
			this.getSafe()
			this.getDetail()
		},
		onLoad: function(options) {
			this.operation = options.operation
			this.id = options.id
      if(options.id) this.disable = true
		},
		data() {
			return {
        disable: false,
				treeData: [{
					label: '一级 1',
					id: 1,
					user: false,
					children: [{
						label: '二级 1-1',
						id: 2,
						user: false,
						children: [{
							id: 130,
							user: true,
							label: '审核人6479',

						}]
					}]
				}, {
					label: '一级 2',
					id: 4,
					user: false,
					children: [{
						id: 5,
						user: false,
						label: '二级 2-1',
						children: [{
							id: 131,
							user: true,
							label: '审核人6478'
						}]
					}, {
						id: 7,
						user: false,
						label: '二级 2-2',
						children: [{
							id: 8,
							user: true,
							label: '三级 2-2-1'
						}]
					}]
				}, {
					label: '一级 3',
					id: 9,
					user: false,
					children: [{
						id: 1,
						user: false,
						label: '二级 3-1',
						children: [{
							id: 11,
							user: true,
							label: '三级 3-1-1'
						}]
					}, {
						label: '二级 3-2',
						id: 12,
						user: false,
						children: [{
							id: 13,
							user: true,
							label: '三级 3-2-1'
						}]
					}]
				}],
				props: { //单选模式(任意一项)
					label: 'label',
					children: 'children',
					multiple: false,
					nodes: true
				},
				show: false,
				showTime: false, //显示日历组件
				showApproval: false, //是否显示选择人员的弹出框
				checkList: [],
				mode: 'single', //日历组件为单个日期
				title: '',
				operation: '', //操作类型 修改(1) 查看(2) 追加（3）审核（4）
				isApproval: false, //是否是审核
				viewDisabled: false, //查看时的禁用选项
				appendDisabled: false, //追加时的禁用选项
				formData: {
					number: '', //作业证编号
					applicant: uni.getStorageSync('userName'), //申请人
					applicationUnit: uni.getStorageSync('userDept'), //申请单位
					level: '', //动火等级
					way: '', //动火方式
					content: '', //动火内容
					startTime: '', //动火开始时间
					endTime: '', //动火结束时间
					// head: '', //动火作业负责人,
					personList: [{
						workerName: '',
						certification: []
					}] //动火人
				},
				formData2Arr: [{
					createTime: '', //第一次动火分析时间
					analyzePoint: '', //分析点名称
					analyzeData: '', //分析数据
					specialWork: '', //涉及的其他特殊作业
					// partPictures: [],
					photo: [],
					analyst: '', //分析人
					identificationResult: ''
				}, ],
				// formData3: {
				// 	applicant: '',
				// 	receiver1: '', //接受交底人1
				// 	receiver2: '', //接受交底人2
				// 	guardian: '' //监护人
				// },
				formData4: {
					licensor: '', //许可人
					signer: '', //签发人
					inCharge: '', //分管负责人
					mainInCharge: '' //主要负责人
				},
				formData5: {},
				rules: {
					'applicant': {
						type: 'string',
						required: true,
						message: '请输入申请人',
						trigger: ['blur', 'change']
					},
				},
				rules2: {},
				rules3: {},
				rules4: {
					'licensor': {
						type: 'string',
						required: true,
						message: '请选择',
						trigger: ['blur', 'change']
					},
					'signer': {
						type: 'string',
						required: true,
						message: '请选择',
						trigger: ['blur', 'change']
					},
				},
				arr: [],
				typeIndex: '',
				timeType: '', //时间是结束时间 还是开始时间
				fileList1: [], //动火人
				statusList: [{
						id: 1,
						name: '合格'
					},
					{
						id: 2,
						name: '不合格'
					},
					{
						id: 3,
						name: '不适用'
					},
				],
				safeMeasuresList: [],
				identificationResult: '',
				receivePersonArr: [], //接受交底人的数组
				specialIndex: '', //涉及的其他特殊作业的下标
				id: '',
				timeValue: Number(new Date()),
				role: '',
				height: '110px',
        licenceLevelArr: []
			}
		},
		methods: {
			open(e) {
				// console.log('open', e)
			},
			close(e) {
				// console.log('close', e)
			},
			change(e) {
				// console.log('change', e)
			},
      getMinDate: function(date) {
        return dayjs(date).valueOf()
      },
			// 如果是修改 查询数据
			getDetail() {
				if (this.id) {
					getDetail({
						id: this.id
					}).then(res => {
						this.formData = res

						this.formData.personList.forEach(item => {
							delete item.createTime
							delete item.updateTime
							delete item.creator
							delete item.updater
							delete item.id
							delete item.tenantId
						})

						this.formData2Arr = res.analyzeDataList
						this.formData2Arr.forEach(item => {
							// delete item.createTime
							delete item.updateTime
							delete item.creator
							delete item.updater
							delete item.id
							delete item.tenantId
						})
						this.safeMeasuresList = res.safeMeasuresList
						this.formData4 = {
							licensor: res.licensor, //许可人
							signer: res.signer, //签发人
							inCharge: res.inCharge, //分管负责人
							mainInCharge: res.mainInCharge //主要负责人
						}
						this.licenceLevelArr.forEach(item => {
							if (item.value == this.formData.level) {
								this.formData.levelLabel = item.label
							}
						})
						this.identificationResult = res.identificationResult

						// 给安全措施的radio赋值
						this.safeMeasuresList.forEach(item => {
							if (item.applicable) {
								this.$set(item, 'radioStatus', '不适用');
							} else {
								if (item.qualified) {
									this.$set(item, 'radioStatus', '合格');
								} else {
									this.$set(item, 'radioStatus', '不合格');
								}
							}
							delete item.createTime
							delete item.updateTime
							delete item.creator
							delete item.updater
							delete item.id
							delete item.tenantId
						})
						// 重新渲染折叠面板的高度
						this.$nextTick(() => {
							this.$refs.workInfo.init()
							this.$refs.safe.init()
							this.$refs.data.init()
						})
					})
				}
			},
			levelSelect(e) {
				this.formData.otherLicenceId = e.id
				this.formData.otherLicenceName = e.name
				this.formData.otherLicenceNumber = e.number
			},
			leftClick() {
				uni.navigateBack({
					delta: 1,
				})
			},
      addPerson() {
        this.formData.personList.push({
          workerName: '',
          certification: [],
          signature: ''
        })
        this.$nextTick(() => {
          this.$refs.workInfo.init()
        })
      },
      deleteRow(index){
        this.formData.personList.splice(index,1)
        this.$nextTick(() => {
          this.$refs.workInfo.init()
        })
      },
            onImgUploadHandler(node) {
                onSnapUploadHandler(node, this);
			},
			select(index, specialIndex) {
        if(this.disable) return false
				// 仅查看时，不可点击修改
				if (!this.viewDisabled) {
					this.show = true; //显示下拉选项
				}

				this.typeIndex = index
				this.specialIndex = specialIndex

				switch (index) {
					case 1:
						this.title = '请选择动火作业级别'
						this.arr = [...this.licenceLevelArr]
						break;
						// case 2:
						// 	this.title = '请选择动火动火方式'
						// 	this.arr = [{
						// 			id: '1',
						// 			name: '方式一'
						// 		},
						// 		{
						// 			id: '2',
						// 			name: '方式二'
						// 		}
						// 	]
						// 	break;
					case 3:
						this.title = '涉及的其他特殊作业'
						this.arr = this.otherLicenceArr
						this.arr.forEach(item => {
							item.name = item.name
						})
						break;
				}
			},
			// 选择时间
			selectTime(index, specialTimeIndex) {
        if(this.disable) return false
				if (!this.viewDisabled) {
					this.showTime = true; //显示下拉选项
				}

				this.timeType = index
				this.specialIndex = specialTimeIndex
			},
			// 确认选择时间
			// 确认选择时间
			confirm(e) {
				let timeValue = this.formatDate(e.value)
				this.showTime = false
				switch (this.timeType) {
					case 1:
						this.formData.startTime = timeValue
						break;
					case 2:
						this.formData.endTime = timeValue
						break;
					case 3:
						this.formData2Arr[this.specialIndex].createTime = timeValue
						break;
				}
			},
			formatDate(str) {
				let date = new Date(str);
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				month = month < 10 ? ('0' + month) : month;
				let day = date.getDate();
				day = day < 10 ? ('0' + day) : day;
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h;
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m;
				let s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s;
				return year + '-' + month + '-' + day + ' ' + h + ':' + m + ':00';
			},
			// 查询涉及的其他特殊作业
			getOtherLicence() {
				getOtherLicence({
					'status': 3
				}).then(res => {
					this.otherLicenceArr = [...res]
				}).catch(err => {
					console.log(err)
				})
			},
			// 获取安全措施配置
			getSafe() {
				if (!this.id) {
					getSafe({
						'type': 3
					}).then(res => {
						this.safeMeasuresList = res
						this.safeMeasuresList.forEach(item => {
							item.radioStatus = ''
						})
						this.$nextTick(() => {
							this.$refs.safe.init()
						})
					}).then(() => {
						// 重新渲染折叠面板的高度
						this.$nextTick(() => {
							this.$refs.workInfo.init()
							this.$refs.safe.init()
              this.$refs.personInfo.init()
              this.$refs.checkPeopleInfo.init()
						})
          }).catch(err => {
						console.log(err)
					})
				}

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event, arr, flag) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = 1
				lists.map((item) => {
					arr.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = arr[1]
					arr.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},

			// 是修改还是查看 还是追加
			isEdit() {
				if (this.operation == 2) {
					this.viewDisabled = true
				} else if (this.operation == 3) {
					this.appendDisabled = true
				} else if (this.operation == 4) {
					this.viewDisabled = true
					this.isApproval = true
				}
			},
			// 流程图
			// 流程图
			toProcess() {
				uni.navigateTo({
					url: `/work/process?id=${this.formData.id}`,
				});
			},
			// 保存
			save() {
				let obj = {} //传给后台的对象
				this.formData.personList.forEach(item => {
					item.certification = item.certification ? item.certification[0] : ''
				})
				this.formData2Arr.forEach(item => {
					item.photo = item.photo ? item.photo[0] : ''
				})
				obj = Object.assign(obj, this.formData, this.formData4, this.formData5)
				obj.analyzeDataList = [...this.formData2Arr]
				obj.safeMeasuresList = [...this.safeMeasuresList]
				obj.submit = false
				obj.type = 3
				obj.safeMeasuresList.forEach(item => {
					if (item.radioStatus == '不适用') {
						item.applicable = true
					} else {
						item.qualified = item.radioStatus == '合格' ? true : false
					}
					delete item.radioStatus
					delete item.deptId
					delete item.id
				})
				let toastObj = {
					type: 'success',
					message: "保存成功",
					url: '/pages/home/homepage',
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/jump.png'
				}

				this.$refs.form4.validate().then(res => {
					if (this.id) {
						editSave(obj).then(res => {
							if (res.code == 0) {
								this.showToast(toastObj)
							}
						}).catch(err => {

						})
					} else {
						addTicket(obj).then(res => {
							if (res.code == 0) {
								this.showToast(toastObj)
							}
						}).catch(err => {

						})
					}
				}).catch(errors => {

				})
			},

			// 提交审核
			submit() {
				let obj = {} //传给后台的对象
				this.formData.personList.forEach(item => {
					item.certification = item.certification ? item.certification[0] : ''
				})
				this.formData2Arr.forEach(item => {
					item.photo = item.photo ? item.photo[0] : ''
				})
				obj = Object.assign(obj, this.formData, this.formData4, this.formData5)
				obj.analyzeDataList = [...this.formData2Arr]
				obj.safeMeasuresList = [...this.safeMeasuresList]
				obj.submit = true
				obj.type = 3

				obj.safeMeasuresList.forEach(item => {
					if (item.radioStatus == '不适用') {
						item.applicable = true
					} else {
						item.qualified = item.radioStatus == '合格' ? true : false
					}
					delete item.radioStatus
					delete item.deptId
					delete item.id
				})

				let toastObj = {
					type: 'success',
					message: "提交成功",
					url: '/pages/home/homepage',
					iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/jump.png'
				}
				this.$refs.form4.validate().then(res => {
					if (this.id) {
						editSave(obj).then(res => {
							if (res.code == 0) {
								this.showToast(toastObj)
							}
						}).catch(err => {

						})
					} else {
						addTicket(obj).then(res => {
							console.log(res)
							if (res.code == 0) {
								this.showToast(toastObj)
							}
						}).catch(err => {

						})
					}
				})
			},
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.switchTab({
							url: params.url,
							fail(err) {
								console.log(err)
							}
						})
					}
				})
			},
			// 打开选择许可人的弹窗
			selectApproval(flag) {
        if(this.disable) return false
				if (!this.viewDisabled) {
					this.showApproval = true
				}
				this.role = flag
			},
			// 关闭选择许可人的弹框
			closeTree() {
				this.showApproval = false
			},
			// 深拷贝
			deepCopy(obj) {
				let newObj = obj.constructor === Array ? [] : {} //判断是深拷贝对象还是数组
				for (let i in obj) {
					if (typeof obj[i] === 'object') {
						newObj[i] = this.deepCopy(obj[i]) //  如果要拷贝的对象的属性依然是个复合类型，递归
					} else {
						newObj[i] = obj[i]
					}
				}
				return newObj
			},
			// 驳回
			rejectWork() {
				this.reject = true
				if (this.formData.rejectReason) {
					let params = {
						licenceId: this.id,
						reasonForRejection: this.formData.rejectReason,
						submit: false
					}
					approval(params).then(res => {
						if (res.code == 0) {
							let toastObj = {
								type: 'success',
								message: "驳回成功",
								url: '/work/todo',
								iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/jump.png'
							}
							this.showToast2(toastObj)
						}
					})
				} else {
					let toastObj = {
						type: 'error',
						message: "请输入驳回原因",
						// url: '/pages/home/homepage',
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/jump.png'
					}
					this.showToast(toastObj)
				}
			},
			// 通过
			approvalAgree() {
				let params = {
					licenceId: this.id,
					submit: true
				}
				approval(params).then(res => {
					if (res.code == 0) {
						let toastObj = {
							type: 'success',
							message: "成功",
							url: '/work/todo',
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/jump.png'
						}
						this.showToast2(toastObj)
					}
				})
			},
			showToast2(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},
			confirmApproval(val) {
				switch (this.role) {
					case 1:
						this.formData4.licensor = val[0].label
						this.formData4.licensorId = val[0].id
						break;
					case 2:
						this.formData4.signer = val[0].label
						this.formData4.signerId = val[0].id
						break;
					case 3:
						this.formData4.inCharge = val[0].label
						this.formData4.inChargeId = val[0].id
						break;
					case 4:
						this.formData4.mainInCharge = val[0].label
						this.formData4.mainInChargeId = val[0].id
						break;
				}
				this.checkList = val
				this.showApproval = false
			},
		},
		watch: {
			'formData.personList': {
				handler(val) {
					this.$refs.personInfo.init()
				},
				deep: true
			},
			'formData.level': {
				handler(val) {
					this.$refs.checkPeopleInfo.init()
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100%;
		height: 100vh;
		overflow: auto;
		background: #efefef;
		padding-bottom: 8vh;
	}

	.showTitle {
		width: 91%;
		margin: 0 auto;
		// font-weight: bold;
		// font-size: 28 rpx;
		padding: 13rpx 20rpx;
		box-sizing: border-box;
		letter-spacing: 5 rpx;
		background: #fff;
		color: #595757;
		margin-top: 35rpx;
		border-radius: 15rpx;
		display: block;

		::v-deep.u-form-item__body {
			display: flex;
			flex-direction: row;
			padding: 5px 0;
		}

		::v-deep.u-line {
			vertical-align: middle;
			display: none;
		}

		// ::v-deep.u-collapse-item__content{
		// 	height: auto !important;
		// }
	}

	.time {
		::v-deep.u-button__text {
			color: rgb(60, 156, 255);
		}
	}

	.btn {
		display: flex;

		// ::v-deep .u-button__text{
		// 	color: red;
		// }
		::v-deep .u-button--success {
			color: #3c9cff !important;
			border-color: #3c9cff;
			background-color: rgba(255, 255, 255, 0) !important;
		}

		::v-deep.u-button--primary {
			color: white !important;
		}
	}

	.line {
		border-bottom: 1px solid #E6E6E6;
	}

	.result {
		display: flex;
		flex-direction: row;
		font-size: 14px;
		color: #303133;
		align-items: center;
	}

	::v-deep .u-input {
		padding: 0px !important;
	}
</style>
